const bcrypt = require('bcrypt');
const HASH_NUM = 8;
/**
 *  @inheritDoc  hash 慢加密 详情见
 *    https://blog.csdn.net/m0_37263637/article/details/80411168
 */

let utils = {
  createServerMsg: (msg, code, data) => {
    return {
      msg,
      error_code: code,
      data: data
    }
  },
  hash_encrypt: (text) => {
    return new Promise(resolve => {
      bcrypt.hash(text, HASH_NUM, function (err, encryptCode) {
        if (!err) {
          resolve(encryptCode)
        }
      })
    })
  },
  hash_compare: (text, encryptCode) => {
    return new Promise(resolve => {
      bcrypt.compare(text, encryptCode, function (err, res) {
        if (!err) {
          resolve(res)
        }
      })
    })
  }
};

module.exports = utils;